const cf = require('../../common_fn/commonFn');
const testModel = require('../../models/test');
const redisCli = require('../../dbs/redisCli').client;
const baiduCli = require('../../SDK/baidu_api/AipImageClassifyClient');
const fs = require('fs');


class Controller {

    async testBaiduCli(ctx,next) { 
        const image = fs.readFileSync("D:/79cd9d1fe46c2fdfc600c734549268d6.jpg").toString("base64");
        // 调用通用物体识别
        await baiduCli.advancedGeneral(image).then(function(result) {
            console.log(result);
            ctx.body = cf.dealSuccessReq(result);
        }).catch(function(err) {
            // 如果发生网络错误
            console.log(err);
            ctx.body = cf.dealErrorReq(err);
        });
    }
    async createTest(ctx,next){

        let m = new testModel({
            testName: '何川'
        });

        await m.save().then((res)=>{
            ctx.body = cf.dealSuccessReq(res);
        }).catch((err)=>{
            ctx.body = cf.dealErrorReq(err);
        });
    }

    async findTest(ctx,next) {
        await testModel.find().then((res)=>{
            ctx.body = cf.dealSuccessReq(res);
        }).catch((err)=>{
            ctx.body = cf.dealErrorReq(err);
        })
    }

    async redisTest(ctx,next) {
        console.log(ctx.request.query)
        await redisCli.set('key1','val1');
        let val = await redisCli.get('key1');
        ctx.body = cf.dealSuccessReq(val);
    }

    async setSessionTest(ctx,next) {
        ctx.session.username = 'hsk';
        console.log(ctx.session)
        ctx.body = cf.dealSuccessReq(ctx.session);
    }
    async getSessionTest(ctx,next) {
        ctx.body = cf.dealSuccessReq(ctx.session);
    }

    async setCookieTest(ctx,next) {
        console.log(ctx.request.body);
        ctx.cookies.set(
            'cid',
            'hello world',
            {
                // domain: 'localhost',  // 写cookie所在的域名
                // path: '/#/user',       // 写cookie所在的路径
                maxAge: 10 * 60 * 10000, // cookie有效时长
                expires: new Date('2019-02-15'),  // cookie失效时间
                httpOnly: false,  // 是否只用于http请求中获取
                overwrite: true  // 是否允许重写
            }
        );
        ctx.body = 'cookie is ok'
    }

    async getCookieTest(ctx,next) {
        ctx.body = cf.dealSuccessReq(ctx.cookies.get('cid'));
    }
}

module.exports = new Controller();

